<!-- 
asyna异步 定义的时候写，调用不用
async :写在function或箭头函数前面
async函数定义：是Generator（生成器）函数的语法糖
async用来修饰函数
async修饰的函数返回Promise实例

await等一下
await只能使用在： ① async函数里，② module的顶级环境里
await 表达式
await后面写promise实例，把异步变同步
async函数的返回值是promise实例
await的作用：在async函数内部把异步变同步

 -->
<script>
  function fn1() {
    return 1;
  }
  async function fn2() {
    return 2;
  }
  function fn3() {
    return new Promise((resolve) => resolve(3));
  }

  function fn4() {
    return Promise.resolve(1);
  }
  // async function fn5(){throw 'error'}

  async function fn5() {
    return Promise.resolve(1);
  }

  function delay(ms) {
    return new Promise(function (resolve) {
      setTimeout(() => resolve(), ms);
    });
  }
  async function fn6() {
    console.log("fn6 开始");
    // const i=await delay(2000).then(()=>1)
    try {
      const i = await delay(2000).then(() => {
        throw 1;
      });
      console.log(i);
    } catch(e) {
      console.log("报错了");
    }
  }

  console.log(fn1()); //1
  console.log(fn2()); //成功的promise,值是2
  console.log(fn3()); //成功的promise,值是3
  console.log(fn4()); //成功的promise,值是1
  console.log(Promise.resolve().then(() => 1));
  // console.log(fn5());
  console.log(fn5());

  console.log(fn6());
</script>
